<?php

namespace JsonLowCode\Form\Fields;

use JsonLowCode\UI\InputNumber;
use JsonLowCode\Form\Abstract\FieldAbstract;
use JsonLowCode\Form\Traits\HasClearable;
use JsonLowCode\Form\Traits\HasDisabled;
use JsonLowCode\Form\Traits\HasPlaceholder;
use JsonLowCode\Form\Traits\HasReadonly;
use JsonLowCode\Form\Traits\HasSize;

/**
 * @property InputNumber $element
 */
class NumberField extends FieldAbstract
{
    use HasDisabled, HasSize, HasPlaceholder, HasClearable, HasReadonly;

    protected string|null $placeholder = null;

    protected function booting(): void
    {
        $this->element = new InputNumber();
    }

    protected function booted(): void
    {
        $this->element->defaultValue(0);
        if (is_string($this->label)){
            $this->placeholder("请输入".$this->label);
        }else{
            $this->placeholder("请输入内容");
        }
    }

    /**
     * 最小值
     * @param float|int|string $step
     * @param bool $isJSExpression
     * @return $this
     */
    public function min(float|int|string $step = 1, bool $isJSExpression = false): static
    {
        $this->element->min($step, $isJSExpression);
        return $this;
    }

    /**
     * 最大值
     * @param float|int|string $step
     * @param bool $isJSExpression
     * @return $this
     */
    public function max(float|int|string $step = 1, bool $isJSExpression = false): static
    {
        $this->element->max($step, $isJSExpression);
        return $this;
    }

    /**
     * 步进值
     * @param float|int|string $step
     * @param bool $isJSExpression
     * @return $this
     */
    public function step(float|int|string $step = 1, bool $isJSExpression = false): static
    {
        $this->element->step($step, $isJSExpression);
        return $this;
    }

    /**
     * 前缀
     * @param mixed $prefix
     * @return $this
     */
    public function prefix(mixed $prefix): static
    {
        $this->element->slotPrefix($prefix);
        return $this;
    }

    /**
     * 后缀
     * @param mixed $suffix
     * @return $this
     */
    public function suffix(mixed $suffix): static
    {
        $this->element->slotSuffix($suffix);
        return $this;
    }


    /**
     * 设置placeholder
     * @param string|null $placeholder
     * @return $this
     */
    public function placeholder(string $placeholder = null): static
    {
        $this->placeholder = $placeholder;
        return $this;
    }

    protected function render(): void
    {
        if ($this->placeholder!=null){
            $this->element->placeholder($this->placeholder);
        }
    }
}
